#include <iostream>
#define data_num 8
using namespace std;

int partition(int data[data_num], int left , int right ){

       int  pivoit = data[left] ;
       while( left < right ){
            while( data[right] > pivoit && left < right )
                right--;
            data[left] = data[right];
            while( data[left] <= pivoit && left < right )
                left++;
            data[right]=data[left];
        }
        data[left] = pivoit;

    cout<<endl;
    for( int i = 0 ; i < data_num; i++){
        cout << data[i] <<"\t";
    }
    cout<<endl;

    return left;
}


int partition_02(int data[], int start, int end){
    int pivot = data[start];
    int left = start, right = end;
    while( left <= right){
        while( left <= right && data[left] <=pivot)
            left++;
        while( left <= right && data[right] > pivot)
            right--;
        if(left < right ){
//            int tmp = data[right];
//            data[right] = data[left];
//            data[left] = data[right];
            swap(data[left] , data[right]);
            left++;
            right--;
        }
    }
    swap(data[start],data[right]);

    cout<<endl;
    for( int i = 0 ; i < data_num; i++){
        cout << data[i] <<"\t";
    }
    cout<<endl;
    return right;
}

void quicksort(int data[] , int left , int right )
{
    if(left<right){
        int p = partition( data , left , right );
        quicksort(data, left , p - 1);
        quicksort(data, p +1, right);
    }
}

void quicksort_02(int data[] , int left ,int right){
    if(left < right ){
        int p = partition_02(data, left,right);
        quicksort_02(data, left, p-1);
        quicksort_02(data , p+1, right);
    }
}

int main()
{
    int data[data_num]={45,32,61,98,74,17,22,53};
//    quicksort( data, 0 ,data_num - 1 );
    quicksort_02( data, 0, data_num - 1);
}
